package com.yztc.yidoutang.activity.main_activity;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.yztc.yidoutang.R;
import com.yztc.yidoutang.activity.result_activity.ResultActivity;
import com.yztc.yidoutang.base.BaseActivity;
import com.yztc.yidoutang.share.SharedURL;

import java.util.List;

public class MainActivity extends BaseActivity implements MainContract.View, View.OnClickListener {
    final int[] bgs = {R.drawable.selector_tuijian, R.drawable.selector_jingxuan, R.drawable.selector_sousuo, R.drawable.selector_wode};
    private ViewPager container;
    private TabLayout mainTablayout;
    private List<Fragment> fragments;
    private EditText searchEditText;
    private LinearLayout searchSelection;
    MainPresenter presenter=new MainPresenter(this);
    private LinearLayout searchBar;
    private TextView soutupian,souquanwu,soudaren,soudanpin;
    private Button cancel;
    private FloatingActionButton fab;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initEvents();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    public void initView() {
        searchBar= (LinearLayout) findViewById(R.id.searchbar);
        mainTablayout = (TabLayout) findViewById(R.id.tb);
        container= (ViewPager) findViewById(R.id.container);
        searchEditText= (EditText) findViewById(R.id.search_edittext);
        searchSelection= (LinearLayout) findViewById(R.id.search_selection);
        soutupian= (TextView) findViewById(R.id.soutupian);
        souquanwu= (TextView) findViewById(R.id.souquanwu);
        soudaren= (TextView) findViewById(R.id.soudaren);
        soudanpin= (TextView) findViewById(R.id.soudanpin);
        cancel= (Button) findViewById(R.id.cancel);
        fab= (FloatingActionButton) findViewById(R.id.fab);
        //初始化自定义TabLayout
        for (int i = 0; i < 4; i++) {
            TabLayout.Tab tab = mainTablayout.newTab();
            View view = LayoutInflater.from(this).inflate(R.layout.main_top_layout, null);
            TextView tv = (TextView) view.findViewById(R.id.main_tv);
            tv.setBackgroundResource(bgs[i]);
            tab.setCustomView(view);
            if (i == 0)
                tv.setFocusable(true);
            mainTablayout.addTab(tab);
        }
        //默认搜索栏和搜索条件选择栏不显示，直到滑动到搜索fragment时才展示
        searchBar.setVisibility(View.GONE);
        searchSelection.setVisibility(View.GONE);
        initViewPager();

    }
    public void initEvents(){
        //将ViewPager的滑动事件监听与TabLayout的切换页面监听绑定
        container.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mainTablayout));
        mainTablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.main_tv).setFocusable(true);
                //判断当前fragment是否是搜索fragment，如果是则展示搜索栏，否则隐藏
                if (tab.getPosition()==2){
                    searchBar.setVisibility(View.VISIBLE);
                    mainTablayout.setVisibility(View.INVISIBLE);
                }else {
                    searchBar.setVisibility(View.GONE);
                    mainTablayout.setVisibility(View.VISIBLE);
                }
                container.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.main_tv).setFocusable(false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        //设置搜索框的TextChanged监听来确定searchSelection视图是否显示
        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchSelection.setVisibility(View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()==0){
                    searchSelection.setVisibility(View.GONE);
                }
            }
        });
        soudanpin.setOnClickListener(this);
        soutupian.setOnClickListener(this);
        souquanwu.setOnClickListener(this);
        soudaren.setOnClickListener(this);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchSelection.getVisibility()==View.VISIBLE){
                    searchSelection.setVisibility(View.GONE);
                }else {
                    container.setCurrentItem(0);
                }

            }
        });
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用相册
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivity(intent);
            }
        });
    }

    public void initViewPager(){
        presenter.initViewPager(MainActivity.this,fragments,container);
    }

    //搜索选择列表的点击事件
    public void onClick(View v) {
        Intent intent=new Intent(MainActivity.this, ResultActivity.class);
        intent.putExtra("keywords", searchEditText.getText().toString().trim());
        switch (v.getId()){
            case R.id.soutupian:
                intent.putExtra("selectionid", SharedURL.SOUTUPIAN_ID);
                break;
            case R.id.souquanwu:
                intent.putExtra("selectionid", SharedURL.SOUQUANWU_ID);
                break;
            case R.id.soudanpin:
                intent.putExtra("selectionid", SharedURL.SOUDAREN_ID);
                break;
            case R.id.soudaren:
                intent.putExtra("selectionid", SharedURL.SOUDANPIN_ID);
                break;

        }
        startActivity(intent);
    }
}
